//
//  OosReviewDao.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/21/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import <oos4ios/DAOs/OosReviewDao.h>
#import <oos4ios/DAOs/OosServiceDao.h>
#import <oos4ios/Oos4ios.h>
#import <oos4ios/Options/OosOrder.h>
#import "../../Atom/Queries/OosReviewQuery.h"
#import "../../Parsers/OosReviewFeedParser.h"
#import "../../Parsers/OosReviewEntryParser.h"
#import "../../Atom/Services/OosAtomService.h"
#import "../../Atom/Feeds/OosServiceFeed.h"
#import "OosDao+Internal.h"


@implementation OosReviewDao

+ (OosReviewDao *) daoFromService:(OosService *)service
{
	return [[[OosReviewDao alloc] initFromService: service] autorelease];
}

+ (OosReviewDao *) daoFromServiceSlug:(NSString *)serviceSlug
{
	return [[[OosReviewDao alloc] initFromServiceSlug: serviceSlug] 
			autorelease];
}

- (OosReviewDao *) initFromService:(OosService *)service
{
	self = [self initFromServiceSlug:[service slug]];
	if (self) {
		self->mService = [service retain];
	}
	return self;
}

- (OosReviewDao *) initFromServiceSlug:(NSString *)serviceSlug
{
	self = [self init];
	if (self) {
		self->mServiceSlug = [serviceSlug retain];
	}
	return self;
}

- (OosDaoTicket *)update: (OosObject *)object 
            withCallback: (id<IOosDaoUpdateCallback>)callback
{
    OosMe *me = [Oos4ios instance].me;
    if (me) {
        // Update is sent to serviceDAo since this one is readonly
        OosServiceDao *delegateDao = [OosServiceDao daoFromUser:me];
        delegateDao.serviceSlug = mServiceSlug;
//        delegateDao.options.reviewOptions.review = (OosReview *) object;
        return [delegateDao update:object withCallback:callback];
    } else {
        return [super update:object withCallback:callback];
    }
}

- (OosDaoTicket *)create:(OosObject *)object 
            withCallback:(id<IOosDaoCreateCallback>)callback
{
    OosMe *me = [Oos4ios instance].me;
    if (me) {
        // Create is sent to serviceDAo since this one is readonly
        OosServiceDao *delegateDao = [OosServiceDao daoFromUser:me];
        
        return [delegateDao create:object withCallback:callback];
    } else {
        return [super create:object withCallback:callback];
    }
}

- (OosQuery *) query
{
	OosQuery *query = [OosReviewQuery query: mServiceSlug];
    self.options.orderOptions.order = [OosOrder contacts];
	[query packStore: self.options];
	return query;
}

- (void)initAtomService:(OosAtomService *)atomService
{
    atomService.feedClass = [OosServiceFeed class];
}

- (id <IOosFeedParser>) feedParser
{
	return [OosReviewFeedParser instance];
}

- (id<IOosEntryParser>)entryParser
{
    return [OosReviewEntryParser instance];
}

- (void)dealloc
{
    [mServiceSlug release];
    [mService release];
    [super dealloc];
}

@end
